import { getTopCategoryAPI } from '@/apis/category'
import { ref, onMounted, onUpdated } from 'vue'
import { useRoute } from 'vue-router'
import {onBeforeRouteUpdate} from 'vue-router'

export function useCategory() {
    const route = useRoute();
    const categoryData = ref({})
    const getTopCategory = async (id = route.params.id) => {
        const res = await getTopCategoryAPI(id);
        categoryData.value = res.result
    }
    onMounted(() => {
        getTopCategory();
    })

    onBeforeRouteUpdate((to) => {
        getTopCategory(to.params.id);
    })
    return {
        categoryData
    }
}